package site.hanzhe.controller;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import site.hanzhe.entity.AdminEntity;
import site.hanzhe.entity.RoleEntity;
import site.hanzhe.service.AdminService;
import site.hanzhe.service.AuthService;
import site.hanzhe.service.RoleService;
import site.hanzhe.utils.R;

import java.lang.reflect.Type;
import java.util.List;

/**
 * 权限分配控制器(负责管理员，角色，权限三者之间的关系)
 */
@Controller
@RequestMapping("/assign")
public class AssignController {

    @Autowired
    private AdminService adminService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private AuthService authService;

    // 跳转到管理员分配角色的页面
    @GetMapping("/to/assign/role/page.html")
    public String toAdminAssignRolePage(Integer id, String current, @RequestParam(value = "query", defaultValue = "") String query, Model model){
        // 查询已分配的角色列表
        List<RoleEntity> assignRole = roleService.selectAssignRoleByAdminId(id);
        // 查询未分配的角色列表
        List<RoleEntity> unAssignRole = roleService.selectUnAssignRoleByAdminId(id);
        // 通过ID查询到管理员详细信息
        AdminEntity entity = adminService.selectAdminById(id);
        // 携带数据跳转到目标页面
        model.addAttribute("assignRole", assignRole);
        model.addAttribute("unAssignRole", unAssignRole);
        model.addAttribute("admin", entity);
        model.addAttribute("query", query);
        model.addAttribute("current", current);
        return "admin-assign-role";
    }

    // 重新分配角色的方法
    @PostMapping("/do/role/assign.html")
    public String doRoleAssign(
            Integer id, String current,
            @RequestParam(value = "query", defaultValue = "") String query,
            @RequestParam(value = "list", defaultValue = "") List<String> list){
        // 清空该管理员下所有的角色
        roleService.deleteAssignRoleByAdminId(id);
        roleService.insertAssignRoleByAdminId(id, list);
        return "redirect:/admin/get/page.html?current=" + current + "&query=" + query;
    }

    // 获取所有权限列表
    @ResponseBody
    @GetMapping("/get/all/auth.json")
    public R selectListAuth(){
        return R.success("获取权限列表成功！").setAttribute("authList", authService.selectList());
    }

    // 查询角色所拥有的所有权限ID
    @ResponseBody
    @GetMapping("/get/assign/auth/by/roleId.json")
    public R selectAssignAuthByRoleId(Integer id){
        return R.success("获取成功！").setAttribute("assignList", authService.selectAssignAuthIdByRoleId(id));
    }

    @PostMapping("/do/role/assign/auth.json")
    @ResponseBody
    public R updateAssignAuthByRoleId(String id, @RequestParam(defaultValue = "") String authList){
        // 通过Gson将Json装换为Java对象
        Gson gson = new Gson();
        Type type = new TypeToken<List<String>>(){}.getType();
        List<String> list = gson.fromJson(authList, type);
        // 删除掉该角色下所有已存在的权限然后重新添加
        authService.deleteAssignAuthByRoleId(id);
        return R.success("分配成功！").setAttribute("count", authService.insertAssignAuthByRoleId(id, list));
    }

}
